package great.easychat.help.uinque;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * sp文件存储
 */
public class SharedPerferencesUtil {
    //sp文件名称
    public final String spFileName = "deviceunique";
    private static SharedPerferencesUtil util;
    private static SharedPreferences mPreferences;

    private SharedPerferencesUtil(Context context) {
        mPreferences = context.getSharedPreferences(spFileName, Context.MODE_PRIVATE);
    }

    /**
     * 初始化SharedPreferencesUtil,只需要初始化一次，建议在Application中初始化
     *
     * @param context 上下文对象
     */
    public static synchronized SharedPerferencesUtil getInstance(Context context) {
        if (util == null) {
            util = new SharedPerferencesUtil(context);
        }
        return util;
    }

    /**
     * 保存数据到SharedPreferences
     *
     * @param key   键
     * @param value 需要保存的数据
     * @return 保存结果
     */
    public boolean putData(String key, Object value) {
        boolean result;
        SharedPreferences.Editor editor = mPreferences.edit();
        String type = value.getClass().getSimpleName();
        try {
            switch (type) {
                case "Boolean":
                    editor.putBoolean(key, (Boolean) value);
                    break;
                case "Long":
                    editor.putLong(key, (Long) value);
                    break;
                case "Float":
                    editor.putFloat(key, (Float) value);
                    break;
                case "String":
                    editor.putString(key, (String) value);
                    break;
                case "Integer":
                    editor.putInt(key, (Integer) value);
                    break;
                default:
                    break;
            }
            result = true;
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        editor.apply();
        return result;
    }

    /**
     * 获取SharedPreferences中保存的数据
     *
     * @param key          键
     * @param defaultValue 获取失败默认值
     * @return 从SharedPreferences读取的数据
     */
    public Object getData(String key, Object defaultValue) {
        Object result = null;
        String type = defaultValue.getClass().getSimpleName();
        try {
            switch (type) {
                case "Boolean":
                    result = mPreferences.getBoolean(key, (Boolean) defaultValue);
                    break;
                case "Long":
                    result = mPreferences.getLong(key, (Long) defaultValue);
                    break;
                case "Float":
                    result = mPreferences.getFloat(key, (Float) defaultValue);
                    break;
                case "String":
                    result = mPreferences.getString(key, (String) defaultValue);
                    break;
                case "Integer":
                    result = mPreferences.getInt(key, (Integer) defaultValue);
                    break;
                default:
                    result = defaultValue;
                    break;
            }
        } catch (Exception e) {
            result = defaultValue;
            e.printStackTrace();
        }
        return result;
    }

    // 移除数据
    public void removeSP(String key) {
        if (mPreferences != null) {
            mPreferences.edit().remove(spFileName);
            mPreferences.edit().commit();
        }
    }
}
